package com.ningxun.pagent.web.controller.aichat;

import com.ningxun.pagent.bus.domain.BusPolicyContent;
import com.ningxun.pagent.bus.domain.aichat.BaseAIChat;
import com.ningxun.pagent.bus.domain.aichat.ChatRequest;
import com.ningxun.pagent.bus.domain.aichat.Query;
import com.ningxun.pagent.bus.domain.wechatuser.UserConversationFavorite;
import com.ningxun.pagent.bus.service.aichat.IBaseAIChatService;
import com.ningxun.pagent.bus.service.basewechatuser.IUserConversationFavoriteService;
import com.ningxun.pagent.common.core.controller.BaseController;
import com.ningxun.pagent.common.core.domain.AjaxResult;
import com.ningxun.pagent.common.core.page.TableDataInfo;
import com.ningxun.pagent.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("/v1")
public class AIChat extends BaseController {

    @Autowired
    private IBaseAIChatService baseAIChatService;

    @Autowired
    private IUserConversationFavoriteService userConversationFavoriteService;

    @PostMapping(value = "/chatMessages", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chatMessages(@RequestBody ChatRequest payload) {
//            传参之前先判断是否为第一次对话输入，如果为第一次，那么就不需要传入conversation_id，否则，需要传入之前对话的conversation_id
        return baseAIChatService.chatMessages(payload);
    }

    @PostMapping(value = "/messageTitle", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult chatMessagesTile(@RequestBody Query query) {
        return AjaxResult.success(baseAIChatService.getResId(query));
    }

    @PostMapping(value = "/messageQuery", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult chatMessagesPredict(@RequestBody Query query) {
        return AjaxResult.success(baseAIChatService.getQuery(query));
    }

    /**
     * 评价对话
     */
    @PutMapping(value = "/evaluateChat")
    public AjaxResult evaluateChat(@RequestBody UserConversationFavorite userConversationFavorite) {

        return AjaxResult.success(baseAIChatService.evaluateChat(userConversationFavorite));
    }

    /**
     * 查看历史ai对话记录
     */
    @GetMapping(value = "/gethistory")
    public TableDataInfo getHistory(BaseAIChat baseAIChat){
        //注意，数据库的user_id名称有误，应该叫open_id
        //这里的username其实存储的也是open_id
        baseAIChat.setUserId(SecurityUtils.getUsername());
        startPage();
        List<BaseAIChat> baseAIChats = baseAIChatService.selectBaseAIChatList(baseAIChat);
        return getDataTable(baseAIChats);
    }



    /**
     * 查看收藏ai对话记录
     */
    @GetMapping(value = "/getfavorite")
    public TableDataInfo getFavorite(UserConversationFavorite userConversationFavorite){
        //注意，数据库的user_id名称有误，应该叫open_id
        //这里的username其实存储的也是open_id
        userConversationFavorite.setUserId(SecurityUtils.getUserId());
        userConversationFavorite.setIsFavorite(true);
        startPage();
        List<UserConversationFavorite> userConversationFavorites = userConversationFavoriteService.selectUserConversationFavoriteList(userConversationFavorite);

        return getDataTable(userConversationFavorites);
    }
}
